package com.zofer.study.linkedlist;

/**
 * @description 反转链表
 * @date: 2020/10/22 18:15
 */
public class Reverse {

    public static void main(String[] args) {
        ListNode listNode3 = new ListNode(4);
        ListNode listNode2 = new ListNode(3);
        listNode2.next = listNode3;
        ListNode listNode1 = new ListNode(2);
        listNode1.next = listNode2;
        ListNode listNode = new ListNode(1);
        listNode.next = listNode1;
        ListNode useNewListNode = changeDireaction(listNode);
        System.out.println("111");
    }

    /**
     * 改变方向
     */
    public static ListNode changeDireaction(ListNode head){
        if(head == null){
            return null;
        }

        ListNode nextNode = head.next;
        head.next = null;
        ListNode tempNode = null;

        while(nextNode != null){
            tempNode = nextNode.next;
            nextNode.next = head;
            head = nextNode;
            nextNode = tempNode;
        }

        return head;
    }


    /**
     * 将原链表中的节点逐个添加进新的链表中
     */
    public static ListNode useNewListNode(ListNode listNode){
        ListNode newListNode = null;
        ListNode pHead = listNode;

        while (pHead != null){
            ListNode temp = pHead.next;
            pHead.next = newListNode;
            newListNode = pHead;
            pHead = temp;

        }
        return newListNode;
    }


    /**
     * 使用stack，1. 先遍历将节点中的值依次加入到stack中  2. 再遍历依次改变链表中各个节点中的值
     */
    public static ListNode useStack(ListNode listNode){

        return null;
    }
}
